package com.example.bishe.controller.gz;

import com.alibaba.fastjson.JSON;
import com.example.bishe.entity.*;
import com.example.bishe.service.Bzks_bjService;
import com.example.bishe.service.RygzService;
import com.example.bishe.service.Zsj_bzksService;
import com.example.bishe.service.Zsj_jzgService;
import com.example.bishe.service.impl.Bzks_bjServiceImpl;
import com.example.bishe.service.impl.RygzServiceImpl;
import com.example.bishe.service.impl.Zsj_bzksServiceImpl;
import com.example.bishe.service.impl.Zsj_jzgServiceImpl;
import com.example.bishe.toolclass.CUD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author yws
 * @Date 2022/4/8 20:53
 */
@RestController
@RequestMapping("/user")
public class UserGuanzhu {
    @Autowired
    Zsj_bzksService bzksService = new Zsj_bzksServiceImpl();
    @Autowired
    RygzService rygzService = new RygzServiceImpl();
    @Autowired
    Bzks_bjService bzks_bjService = new Bzks_bjServiceImpl();
    @Autowired
    CUD cud;

    @PostMapping("/student")
    public String addStudent(@RequestBody User user){
        int errcode = 0;//错误代码1001
        String errmsg =" ";//错误描述
        if(user.getUserid()==null){
            errcode = 1001;
            errmsg+="学号不能为空,";
        }
        if(user.getName()==null)
        {
            errcode = 1001;
            errmsg+="姓名不能为空,";
        }
        /*首先判断电话号码是否为空*/
        if(user.getPhone()==null){
            errcode = 1001;
            errmsg+="电话号码不能为空,";
        }
        if(errmsg.length()>1){
            errcode = 1001;
            errmsg = errmsg.substring(1, errmsg.length() - 1) + "。";
        }else {
            /*String x = "1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}";
            * */
            String x = "1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}";
            if(user.getPhone().matches(x)){
                /*判断数据库是否存在这个学生信息*/
                Zsj_bzks bzks = new Zsj_bzks();
                bzks.setXh(user.getUserid());bzks.setXm(user.getName());
                Zsj_bzks[] zsjBzks = bzksService.select_bzks(bzks);
                if(zsjBzks.length==0){
                    errcode = 1001;
                    errmsg+="你输入的用户不存在,请确认用户信息正确。";
                }else {
                    bzks = zsjBzks[0];
                    /*看用户是否已经关注*/
                    Rygz rygz = new Rygz();
                    rygz.setUserid(user.getUserid());rygz.setUsername(user.getName());
                    Rygz[] rygzs = rygzService.selectRygz(rygz);
                    if(rygzs.length>0){
                        errcode = 1001;
                        errmsg+="你已关注过企业微信，手机号为："+rygzs[0].getPhone()+" 时间为："+rygzs[0].getGztime();
                    }else {
                        Bzks_bj bzks_bj = new Bzks_bj();
                        bzks_bj.setBh(zsjBzks[0].getBh());
                        /**/
                        Bzks_bj[] bzks_bjs = bzks_bjService.select_bj(bzks_bj);
                        /*执行关注操作*/
                        System.out.println(zsjBzks[0].getXbdm()+" "+user.getEmail()+" "+bzks_bjs[0].getWx_id()+" "+user.getPhone());
                        HashMap value = cud.addstudent_jzg(bzks.getXh(), bzks.getXm(), zsjBzks[0].getXbdm() + "",
                                user.getEmail(), bzks_bjs[0].getWx_id() + "", user.getPhone());
                        if (value.get("errcode").equals("0")) {
                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            rygz.setPhone(user.getPhone());rygz.setGztime(df.format(new java.util.Date()));
                            rygzService.addRygz(rygz);/*添加关注的信息，数据库中*/
                            bzks.setEmail(user.getEmail());
                            bzks.setTelphone(user.getPhone());
                            bzksService.query_bzks(bzks);
                            errcode=0;
                            errmsg="恭喜你成功关注中工学子。";
                        } else {
                            errmsg+= cud.addstudent_jzgpan(value);
                            errcode=1001;
                        }

                    }
                }
            }
            else {
                errcode = 1001;
                errmsg+="电话号码的格式错误，请你确定电话为正确的。";
            }
            errmsg = errmsg.substring(1, errmsg.length());
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }
    @Autowired
    Zsj_jzgService jzgService = new Zsj_jzgServiceImpl();
    @PostMapping("/teacher")
    public String addTeacher(@RequestBody User user){
        int errcode = 0;//错误代码1001
        String errmsg =" ";//错误描述
        if(user.getUserid()==null){
            errcode = 1002;
            errmsg+="工号不能为空,";
        }
        if(user.getName()==null)
        {
            errcode = 1002;
            errmsg+="姓名不能为空,";
        }
        /*首先判断电话号码是否为空*/
        if(user.getPhone()==null){
            errcode = 1002;
            errmsg+="电话号码不能为空,";
        }
        if(errmsg.length()>1){
            errcode = 1001;
            errmsg = errmsg.substring(1, errmsg.length() - 1) + "。";
        }else {
            String x = "1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}";
            if(user.getPhone().matches(x)){
                /*判断数据库是否存在这个教师信息*/
                /*Zsj_bzks bzks = new Zsj_bzks();
                bzks.setXh(user.getUserid());bzks.setXm(user.getName());
                Zsj_bzks[] zsjBzks = bzksService.select_bzks(bzks);*/

                Zsj_jzg jzg = new Zsj_jzg();
                jzg.setZgh(user.getUserid());jzg.setXm(user.getName());
                Zsj_jzg[] selectdw = jzgService.selectdw(jzg);
                if(selectdw.length==0){
                    errcode = 1002;
                    errmsg+="你输入的用户不存在,请确认用户信息正确。";
                }else {
                    jzg = selectdw[0];
                    /*看用户是否已经关注*/
                    Rygz rygz = new Rygz();
                    rygz.setUserid(user.getUserid());rygz.setUsername(user.getName());
                    Rygz[] rygzs = rygzService.selectRygz(rygz);
                    if(rygzs.length>0){
                        errcode = 1002;
                        errmsg+="你已关注过企业微信，手机号为："+rygzs[0].getPhone()+" 时间为："+rygzs[0].getGztime();
                    }else {
                        /*执行关注操作*/
                        HashMap value = cud.addstudent_jzg(jzg.getZgh(),jzg.getXm(),jzg.getXbdm()+""
                                ,jzg.getEmail(),jzg.getZxbz_dw().getWx_id()+"", user.getPhone());
                        if (value.get("errcode").equals("0")) {
                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            rygz.setPhone(user.getPhone());rygz.setGztime(df.format(new java.util.Date()));
                            rygzService.addRygz(rygz);/*添加关注的信息，数据库中*/
                            jzg.setEmail(user.getEmail());
                            jzg.setSjh(user.getPhone());
                            jzgService.query_jzg(jzg);
                            errcode=0;
                            errmsg="恭喜你成功关注中工学。";
                        } else {
                            errmsg+= cud.addstudent_jzgpan(value);
                            errcode=1002;
                        }

                    }
                }
            }
            else {
                errcode = 1002;
                errmsg+="电话号码的格式错误，请你确定电话为正确的。";
            }
            errmsg = errmsg.substring(1, errmsg.length());
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/selectgz")
    public String selectGz(@RequestBody Rygz rygz){
        Rygz[] rygzs = rygzService.selectRygz(rygz);
        List<Rygz> list = new ArrayList<>();
        for (int i = 0; i < rygzs.length; i++) {
            list.add(rygzs[i]);
        }
        String s = JSON.toJSONString(list);
        System.out.println(s);
        return s;
    }

}
